package com.itheima.d6_threadhomework0910.num3;

import java.util.List;
import java.util.Random;

/**
 * 线程任务类
 */

public class CJTask implements Runnable {
    //定义一个集合 用作抽奖池
    private List<String> prizeList;

    public CJTask(List<String> prizeList) {
        this.prizeList = prizeList;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        Random r = new Random();
        synchronized (this) {
            while (prizeList.size() > 0) {
                if(prizeList.size() <= 0){
                    break;
                }
                //随机从集合中抽取一个奖品
                String prize = prizeList.get(r.nextInt(prizeList.size()));
                //从集合中删除这个
                prizeList.remove(prize);
                //打印抽出的奖品
                System.out.println(name + "抽出了一个...:" + prize);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
